package com.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.lang3.time.DateFormatUtils;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	public static String YYYY = "yyyy";
	public static String YYYY_MM = "yyyy-MM";
	public static String YYYY_MM_DD = "yyyy-MM-dd";
	public static String YYYYMMDD = "yyyyMMdd";
	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
//	private static final String FORMAT_0 = "yyyy-MM-dd HH:mm:ss";
//	private static final String FORMAT_1 = "yyyy-MM-dd";
//	private static final String FORMAT_2 = "HH:mm:ss";
	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
			"yyyy.MM.dd HH:mm", "yyyy.MM" };

	public static Date praseNowDate() {
		String dateStr = getDate();
		return dateTime(YYYY_MM_DD, dateStr);
	}

	public static Date getNowDate() {
		return new Date();
	}

	public static String getDate() {
		return dateTimeNow(YYYY_MM_DD);
	}

	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}

	public static final String getTime() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(YYYYMMDDHHMMSS);
	}

	public static final String dateTimeNow(String format) {
		return parseDateToStr(format, new Date());
	}

	public static final String dateTime(Date date) {
		return parseDateToStr(YYYY_MM_DD, date);
	}

	public static final String parseDateToStr(String format, Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	public static final Date dateTime(String format, String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd");
	}

	public static final String dateTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyyMMdd");
	}

	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
		}
		return null;
	}

	public static SimpleDateFormat returnSimple(String date) {
		SimpleDateFormat simple = null;
		switch (date.trim().length()) {
		case 21:
			simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
			break;
		case 19:
			simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 10:
			simple = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 8:
			simple = new SimpleDateFormat("HH:mm:ss");
			break;
		}
		return simple;
	}

	public static Date reverse2Date(String date) {
		SimpleDateFormat simple = returnSimple(date);
		try {
			return simple.parse(date.trim());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static java.sql.Date reverse2SqlDate(String date) {
		SimpleDateFormat simple = returnSimple(date);
		try {
			return new java.sql.Date(simple.parse(date.trim()).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if ((pattern != null) && (pattern.length > 0)) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	public static String formatDateTime(Date date) {
		return formatDate(date, new Object[] { "yyyy-MM-dd HH:mm:ss" });
	}

	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / 86400000L;
	}

	/**
	 * 两时间段相隔秒数
	 */
	public static double getDistanceOfTwoDateByMinu(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / 1000 / 60;
	}

	public static Date beforeMonthDate(int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(2, -month);
		return c.getTime();
	}

	public static Date laterMonthDate(Date date, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(2, month);
		return c.getTime();
	}

	public static Date beforeDayDate(int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(5, -day);
		return c.getTime();
	}

	public static Date beforeDayNewDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		if (date == null) {
			c.setTime(new Date());
		} else {
			c.setTime(date);
		}
		c.add(5, -day);
		return c.getTime();
	}

	public static String convert(String s, String s1, String s2) {
		Date date = getDate(s, s1);
		if (null == date) {
			return "";
		}
		return getDateStr(date, s2);
	}

	public static String getDateStr(Date date, String s) {
		SimpleDateFormat simpledateformat = new SimpleDateFormat(s);
		return simpledateformat.format(date);
	}

	public static Date getDate(String s, String s1) {
		return getDate(s, s1, 0L);
	}

	public static Date getDate(String s, String s1, long l) {
		SimpleDateFormat simpledateformat = new SimpleDateFormat(s1);
		Date date;
		try {
			date = simpledateformat.parse(s);
		} catch (Exception _ex) {
			date = new Date(l);
		}
		return date;
	}

	public static long residueMin(Date date1, Date date2) {
		long t = date2.getTime() - date1.getTime();
		return t / 60000L;
	}

	public static Date changeTimeZone(Date date, TimeZone oldZone, TimeZone newZone) {
		Date dateTmp = null;
		if (date != null) {
			int timeOffset = oldZone.getRawOffset() - newZone.getRawOffset();
			dateTmp = new Date(date.getTime() - timeOffset);
		}
		return dateTmp;
	}

	public static String getWeekByTime(String format) {
		try {
			return formatDate(parseDate(format, parsePatterns), new Object[] { "E" });
		} catch (ParseException e) {
		}
		return null;
	}

	public static Date getNetworkTime(String webUrl) {
		try {
			URL url = new URL(webUrl);
			URLConnection conn = url.openConnection();
			conn.connect();
			long dateL = conn.getDate();
			return new Date(dateL);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Long getUnixTimes(Date date) {
//		Instant instant = date.toInstant();
//		ZoneId zoneId = ZoneId.systemDefault();

//		LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

		Long second = Long.valueOf(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
		return second;
	}

	public static Long getSecondTimes(Date date) {
		return date.getTime() / 1000;
	}

	/**
	 * 获取当日最晚时间
	 * 
	 * @return
	 */
	public static Date getTodayLastTimes() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
				59, 59);
		Date endOfDate = calendar.getTime();
		return endOfDate;
	}

	/**
	 * 获取num天前最早时间
	 * 
	 * @return
	 */
	public static Date getDayStartTimes(Integer num) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE) - num, 00,
				00, 00);
		Date startOfDate = calendar.getTime();
		return startOfDate;
	}

	/**
	 * 获取num天前最早时间
	 * 
	 * @return
	 */
	public static Date getDayEndTimes(Integer num) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE) - num, 23,
				59, 59);
		Date startOfDate = calendar.getTime();
		return startOfDate;
	}

	/**
	 * 获取当前时间之前的天数
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static List<String> getDecreaseDateString(Integer num) {
		List<String> dates = new ArrayList<>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < num; i++) {
			calendar.setTime(date);
			calendar.add(Calendar.DATE, -i);
			String d = sdf.format(calendar.getTime());
			dates.add(d);
		}
		return dates;
	}

	/**
	 * 获取指定时间之前的天数
	 * 
	 * @param date
	 * @param num
	 * @return
	 */
	public static List<String> getDecreaseDateString(Date date, Integer num) {
		List<String> dates = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < num; i++) {
			calendar.setTime(date);
			calendar.add(Calendar.DATE, -i);
			String d = sdf.format(calendar.getTime());
			dates.add(d);
		}
		return dates;
	}

	/**
	 * 获取当月之前的月份
	 * 
	 * @param num
	 * @return
	 */
	public static List<String> getDecreaseMonthString(Integer num) {
		List<String> dates = new ArrayList<>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar calendar = Calendar.getInstance();
		for (int i = 0; i < num; i++) {
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, -i);
			String d = sdf.format(calendar.getTime());
			dates.add(d);
		}
		return dates;
	}

	/**
	 * 根据两个日期获取相差天数 时间格式 2019-01-01
	 * 
	 * @return
	 */
	public static int getDayByTime(String endDate, String startDate) {
		String[] endDates = endDate.split("-");
		int endYear = StringUtils.toInteger(endDates[0]);
		int endMonth = StringUtils.toInteger(endDates[1]);
		int endDay = StringUtils.toInteger(endDates[2]);
		Calendar c1 = Calendar.getInstance();
		c1.set(endYear, endMonth, endDay);

		String[] startDates = startDate.split("-");
		int startYear = StringUtils.toInteger(startDates[0]);
		int startMonth = StringUtils.toInteger(startDates[1]);
		int startDay = StringUtils.toInteger(startDates[2]);
		Calendar c2 = Calendar.getInstance();
		c2.set(startYear, startMonth, startDay);

		long n1 = c1.getTimeInMillis();
		long n2 = c2.getTimeInMillis();
		return (int) Math.abs((n1 - n2) / 24 / 3600000);
	}

	/**
	 * 时间比较 如果date1>date2,返回true
	 * 
	 * @param date1
	 * @param date2
	 */
	public static boolean compareDate(Date date1, Date date2) {
		return date1.getTime() > date2.getTime();
	}

	public static Date strToDate(String dateStr, String formatStr) {
		DateFormat dd = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = dd.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将“20130307164931000+0800”格式的时间转换成Date类型
	 * 
	 * @param str
	 * @return
	 */
	public static Date strToDate3(String str) {
		String formatStr = "yyyyMMddHHmmssSSS";
		return strToDate(str, formatStr);
	}

	public static Date utc2Local(String utcTime, String utcTimePatten) {
		SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
		utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
		Date gpsUTCDate = null;
		try {
			gpsUTCDate = utcFormater.parse(utcTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return gpsUTCDate;
	}

	/**
	 * 根据毫数返回Date
	 * 
	 * @param millisecond
	 * @return
	 */
	public static Date getDateByMillisecond(long millisecond) {
		Calendar ca = Calendar.getInstance();
		ca.setTimeInMillis(millisecond);
		return ca.getTime();
	}

	/**
	 * 北京时间转成洛杉矶时间
	 * 
	 * @parama
	 * @return
	 */
	public static Date getLosDateFromBeijingDate(Date d) {
		if (d == null) {
			return null;
		}
		String formatStr = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat formater = new SimpleDateFormat(formatStr);
		formater.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
		String losTime = formater.format(d);
		return strToDate(losTime, formatStr);
	}

	/**
	 * 根据毫秒返回洛杉矶Date
	 * 
	 * @param millisecond
	 * @return
	 */
	public static Date getLosDateByMilliSecond(long millisecond) {
		Date d = getDateByMillisecond(millisecond);
		return getLosDateFromBeijingDate(d);
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 获取当前日期后面的日期 type 月份或年 date 在这个时间基础上增加 num 要调整的天数
	 * 
	 * @return
	 */
	public static Date getNextDate(String type, Date date, int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);// 设置起时间
		if (type.equals("DAY")) {
			// 天数
			cal.add(Calendar.DAY_OF_MONTH, num);
		} else if (type.equals("MONTH")) {
			// 月份
			cal.add(Calendar.MONTH, num);
		} else if (type.equals("YEAR")) {
			// 年份
			cal.add(Calendar.YEAR, num);
		} else if (type.equals("HOUR")) {
			// 小时
			cal.add(Calendar.HOUR_OF_DAY, num);
		} else if (type.equals("MINUTE")) {
			// 分钟
			cal.add(Calendar.MINUTE, num);
		}
		return cal.getTime();
	}

	/**
	 * 判断该日期是否是该月的最后一天
	 * 
	 * @param date 需要判断的日期
	 * @return
	 */
	public static boolean isLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 将“2014-09-16T14:54:34”格式的时间转换成Date类型
	 * 
	 * @param str
	 * @return
	 */
	public static Date strToDate2(String str) {
		str = str.replace("T", " ");
		String formatStr = "yyyy-MM-dd HH:mm:ss";
		return strToDate(str, formatStr);
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param str
	 * @return
	 */
	public static Date strToDate4(String str) {
		String formatStr = "yyyy-MM-dd";
		return strToDate(str, formatStr);
	}

	public static String dateToString(Date date, String formatStr) {
		DateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(date);
	}

	/**
	 * 北京时间转换为UTC时间
	 * 
	 * @param d
	 * @return
	 */
	public static Date getUtcDateFromBeijingDate(Date d) {
		String formatStr = "yyyy-MM-dd HH:mm:ss";
		return getUtcDateFromDate(d, formatStr);
	}

	public static Date getUtcDateFromDate(Date d, String formatStr) {
		return strToDate1(getUtcStrFromDate(d, formatStr));
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param str
	 * @return
	 */
	public static Date strToDate1(String str) {
		String formatStr = "yyyy-MM-dd HH:mm:ss";
		return strToDate(str, formatStr);
	}

	public static String getUtcStrFromDate(Date d, String formatStr) {
		if (d == null) {
			return null;
		}
		SimpleDateFormat utcFormater = new SimpleDateFormat(formatStr);
		utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
		return utcFormater.format(d.getTime());
	}

	/**
	 * 将某个日期增加指定天数，并返回结果 如果传入负数，则为减
	 * 
	 * @param date    要操作的日期对象
	 * @param ammount 要增加天的数
	 * @return 结果日期对象
	 */
	public static Date addDay(final Date date, final int ammount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, ammount);
		return c.getTime();
	}

	/**
	 * 将某个日期增加指定天数，并返回结果 如果传入负数，则为减
	 * 
	 * @param date    要操作的日期对象
	 * @param ammount 要增加天的数
	 * @return 结果日期对象
	 */
	public static Date timeConverDate(Long timeStamp) {
		Date date1 = new Date(timeStamp * 1000);
		return date1;
	}

	/**
	 * 将某个日期增加指定秒，并返回结果 如果传入负数，则为减
	 * 
	 * @param date    要操作的日期对象
	 * @param ammount 要增加秒数
	 * @return 结果日期对象
	 */
	public static Date addSecond(final Date date, final int ammount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.SECOND, ammount);
		return c.getTime();
	}

	public static String getPlatformDate(Date date, String countryShortName) {
		Instant instant = date.toInstant();
		String shortName = countryShortName.toUpperCase();
		LocalDateTime localDateTime = LocalDateTime.now(ZoneId.systemDefault());
		if (shortName.equals("US")) {
			localDateTime = instant.atZone(ZoneId.of("America/Los_Angeles")).toLocalDateTime();
		} else if (shortName.equals("DE")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/Berlin")).toLocalDateTime();
		} else if (shortName.equals("GB")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/London")).toLocalDateTime();
		} else if (shortName.equals("CA")) {
			localDateTime = instant.atZone(ZoneId.of("America/Vancouver")).toLocalDateTime();
		} else if (shortName.equals("MX")) {
			localDateTime = instant.atZone(ZoneId.of("America/Mexico_City")).toLocalDateTime();
		} else if (shortName.equals("BR")) {
			localDateTime = instant.atZone(ZoneId.of("Brazil/East")).toLocalDateTime();
		} else if (shortName.equals("ES")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/Madrid")).toLocalDateTime();
		} else if (shortName.equals("FR")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/Paris")).toLocalDateTime();
		} else if (shortName.equals("IN")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Calcutta")).toLocalDateTime();
		} else if (shortName.equals("IT")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/Rome")).toLocalDateTime();
		} else if (shortName.equals("TR")) {
			localDateTime = instant.atZone(ZoneId.of("Turkey")).toLocalDateTime();
		} else if (shortName.equals("AU")) {
			localDateTime = instant.atZone(ZoneId.of("Australia/Victoria")).toLocalDateTime();
		} else if (shortName.equals("JP")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Tokyo")).toLocalDateTime();
		} else if (shortName.equals("AE")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Dubai")).toLocalDateTime();
		} else if (shortName.equals("NL")) {
			localDateTime = instant.atZone(ZoneId.of("Europe/Amsterdam")).toLocalDateTime();
		} else if (shortName.equals("ID")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Jakarta")).toLocalDateTime();
		} else if (shortName.equals("MY")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Kuala_Lumpur")).toLocalDateTime();
		} else if (shortName.equals("TH")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Bangkok")).toLocalDateTime();
		} else if (shortName.equals("VN")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Ho_Chi_Minh")).toLocalDateTime();
		} else if (shortName.equals("PH")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Manila")).toLocalDateTime();
		} else if (shortName.equals("SG")) {
			localDateTime = instant.atZone(ZoneId.of("Asia/Singapore")).toLocalDateTime();
		}
		String localDateTimes = localDateTime.toString() + "Z";
		return localDateTimes;
	}
	
	public static boolean isThisTime(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String now = sdf.format(new Date());//当前时间
        if (date.equals(now)) {
            return true;
        }
        return false;
    }
	
	public static String getMonthLastDay(String date) {
        if(isThisTime(date, "yyyy-MM")) {
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String now = sdf.format(new Date());
            return now.split("-")[2];
        }else {
        	String[] dates = date.split("-");
        	Calendar cal = Calendar.getInstance();
        	cal.set(Calendar.YEAR, Integer.valueOf(dates[0]));
    		// 设置月份
    		cal.set(Calendar.MONTH, Integer.valueOf(dates[1])-1); //设置当前月的上一个月
        	return String.valueOf(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        }
    }
	
	/**
	 * 获取指定月份第一天
	 * @return
	 */
	public static String getFirstDayByMonth(Date date) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(cale.getTime());
	}
	/**
	 * 获取指定月份最后一天
	 * @return
	 */
	public static String getLastDayByMonth(Date date) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    cale.add(Calendar.MONTH, 1);
	    cale.set(Calendar.DAY_OF_MONTH, 0);
		return format.format(cale.getTime());
	}
	
	/**
     * 根据region 获取平台当地时间
     * @param region
     * @param amount  amont为空或者0取当地当前时间，如果大于0或者小于0取档期时间加上或者小于amont天数的时间。
     * @return
     */
    public static String getPlatformDateByRegion(String region, Integer amount) {
    	Date date = new Date();
    	if(amount == null || amount == 0) {
    		date = addMinutes(date, -10);//防止亚马逊当读服务器时间不一致，提前延后10分钟作为当地直接。
    	} else {
    		date = addDay(date, amount);
    	}
		String localDateTime = getISO8601Timestamp(date, "EST");
		if (region.equals("NA")) {//北美
			
		} else if (region.equals("EU")) {//欧洲
			localDateTime = getISO8601Timestamp(date, "UTC");
		} else if (region.equals("FE")) {//远东
			localDateTime = getISO8601Timestamp(date, "PST");
		}
		return localDateTime;
	}
    
    public static String getISO8601Timestamp(Date date, String timeZone){  
        TimeZone tz = TimeZone.getTimeZone(timeZone);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
        df.setTimeZone(tz);  
        String nowAsISO = df.format(date);  
        return nowAsISO;  
    }

	public static void main(String[] args) {
		System.out.println(getMonthLastDay("2019-02"));
		
//		String month = "1";
//		//获取前月的第一天
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
//        //获取当前月第一天：
//        Calendar c = Calendar.getInstance();
//
//        String first = null;//第一天
//        String last = null;//第二天
//        //判断获取的参数如果为-1
//        if(month.equals("-1")){
//            c.add(Calendar.MONTH, -1);
//            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
//            first = sdf.format(c.getTime());
//            System.out.println("last=="+first);
//
//            //获取上月的最后一天
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.DAY_OF_MONTH, 1); 
//            calendar.add(Calendar.DATE, -1);
//            last = sdf.format(calendar.getTime());
//            System.out.println("last=="+last);
//        }else{
//            c.add(Calendar.MONTH, 0);
//            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
//            first = sdf.format(c.getTime());
//            System.out.println(first);
//
//            //获取当前月最后一天
//            Calendar ca = Calendar.getInstance();    
//            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
//            last = sdf.format(ca.getTime());
//            System.out.println(last);
//        }
	}
}
